package com.example.qqlaobing.Umoney.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qqlaobing.Umoney.R;
import com.example.qqlaobing.Umoney.been.SelectProductsBean;
import com.example.qqlaobing.Umoney.net.NetRequestBusinessDefine;
import com.example.qqlaobing.Umoney.net.NetRequestRULBuilder;
import com.example.qqlaobing.Umoney.presenter.presenterImpl.BankPresenterImpl;
import com.example.qqlaobing.Umoney.presenter.presenterImpl.LoanMoneyNextPresenterImpl;
import com.example.qqlaobing.Umoney.presenter.presenterImpl.SelectProductPresenterImpl;
import com.example.qqlaobing.Umoney.view.fragment.bean.AppLyLoan;
import com.example.qqlaobing.Umoney.view.fragment.home.act.Act_LoanRecognition;
import com.example.qqlaobing.Umoney.view.myview.LoadingDialog;
import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;

public class LoanActivity1 extends AppCompatActivity implements View.OnClickListener {
    private Button btnLoan;
    private CheckBox checkBox;
    private TextView tvGetMoney, tvSureLoanMoney;
    private Spinner tvLoanTime;
    private ImageView ivCancel;
    private List<String> data_list = new ArrayList<>();
    private ArrayAdapter<String> arr_adapter;
    private String day;
    private String money;
    private TextView tv_bank, tv_fenqi;
    private TextView tv_danqi;
    private TextView tv_agree;
    private String product_id, backid;
    private LoadingDialog dialog;
    private TextView tishi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_loan);
        StatusBarCompat.setStatusBarColor(this,getResources().getColor(R.color.title_bar));
        initView();
        initData();
    }

    private void initView() {
        tvGetMoney = (TextView) findViewById(R.id.tv_getmoney);
        tvLoanTime = (Spinner) findViewById(R.id.tv_loantime);
        tv_bank = (TextView) findViewById(R.id.tv_bank);
        tv_danqi = (TextView) findViewById(R.id.tv_danqi);
        tv_fenqi = (TextView) findViewById(R.id.tv_fenqi);
        tv_agree = (TextView) findViewById(R.id.tv_agree);
        checkBox = (CheckBox) findViewById(R.id.cb_checkbox);
        btnLoan = (Button) findViewById(R.id.btn_loan);
        tishi = findViewById(R.id.tishi);
        ivCancel = (ImageView) findViewById(R.id.iv_cancel);
        tv_fenqi.setOnClickListener(this);
        tv_danqi.setOnClickListener(this);
        ivCancel.setOnClickListener(this);
        tv_danqi.setSelected(true);
        tv_fenqi.setSelected(false);
    }

    private void initData() {
        btnLoan.setOnClickListener(this);
        //适配器
        arr_adapter = new ArrayAdapter<String>(LoanActivity1.this, android.R.layout.simple_spinner_item, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        tvLoanTime.setAdapter(arr_adapter);
        tvLoanTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                day = data_list.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        postselectProducts();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_loan:
                if (tv_danqi.getText().toString().equals("") || tv_bank.getText().toString().equals("")) {
                    Toast.makeText(LoanActivity1.this, "请完善信息", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent1 = new Intent(LoanActivity1.this, Act_LoanRecognition.class);
//                    Intent intent = new Intent(LoanActivity1.this,LoanNextActivity.class);
                    startActivity(intent1);
                }
                break;
            case R.id.iv_cancel:
                finish();
                break;
            case R.id.tv_agree:
                Intent intent1 = new Intent(LoanActivity1.this, AgreeWebActivity.class);
                startActivity(intent1);
                break;
            case R.id.tv_fenqi://分期
                if (appLyLoan == null) {
                    return;
                }
//                tv_danqi.setSelected(false);
//                tv_fenqi.setSelected(true);
                data_list.clear();
                for (int i = 0; i < appLyLoan.getData().getData().getProducts().size(); i++) {
                    if (appLyLoan.getData().getData().getProducts().get(i).getId().equals("3")) {//当前
                        List<String> list = Arrays.asList(appLyLoan.getData().getData().getProducts().get(i).getDays().split(","));
                        for (int j = 0; j < list.size(); j++) {
                            data_list.add(list.get(j));
                        }
                    }
                }
                arr_adapter.notifyDataSetChanged();
                break;
            case R.id.tv_danqi://当前
                if (appLyLoan == null) {
                    return;
                }
//                tv_danqi.setSelected(true);
//                tv_fenqi.setSelected(false);
                data_list.clear();
                for (int i = 0; i < appLyLoan.getData().getData().getProducts().size(); i++) {
                    if (appLyLoan.getData().getData().getProducts().get(i).getId().equals("2")) {//分期
                        List<String> list = Arrays.asList(appLyLoan.getData().getData().getProducts().get(i).getDays().split(","));
                        for (int j = 0; j < list.size(); j++) {
                            data_list.add(list.get(j));
                        }
                    }
                }
                arr_adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(LoanActivity1.this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private SelectProductsBean appLyLoan;

    /**
     * 获取年限选择
     */
    public void postselectProducts() {
        OkHttpUtils
                .post()
                .url(NetRequestRULBuilder.buildRequestUrl(NetRequestBusinessDefine.K_selectProducts))
                .addHeader("token", NetRequestBusinessDefine.K_TOKEN)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        appLyLoan = gson.fromJson(response, SelectProductsBean.class);
                        if (appLyLoan.getStatus() == 1) {
                            for (int i = 0; i < appLyLoan.getData().getData().getProducts().size(); i++) {
                                if (appLyLoan.getData().getData().getProducts().get(i).getId().equals("2")) {//分期
                                    data_list.clear();
                                    List<String> list = Arrays.asList(appLyLoan.getData().getData().getProducts().get(i).getDays().split(","));
                                    for (int j = 0; j < list.size(); j++) {
                                        data_list.add(list.get(j));
                                    }
                                    tv_danqi.setText("￥" + appLyLoan.getData().getData().getProducts().get(i).getAmount());
                                } else {//当前
                                    tv_fenqi.setText("￥" + appLyLoan.getData().getData().getProducts().get(i).getAmount());
                                }
                            }
                            arr_adapter.notifyDataSetChanged();
                            tishi.setText(appLyLoan.getData().getData().getTips());
                            tv_bank.setText(appLyLoan.getData().getData().getBanks().get(0).getBankname() + ":" + appLyLoan.getData().getData().getBanks().get(0).getBankno());
                        } else {
                            Toast.makeText(getApplicationContext(), "网络异常！", Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }
}
